﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Aliyeye.Car.Net.UI.SystemHelp;
using System.Data;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;

namespace Aliyeye.Car.Net.UI.PrintExport
{
    public class WordHelper
    {
        Word.Application wordApp = null;
        object readOnly = false;
        object myTrue = false;
        object myFalse = false;
        object missingValue = Type.Missing;
        object range = Word.WdPrintOutRange.wdPrintAllDocument;
        object items = Word.WdPrintOutItem.wdPrintDocumentContent;
        object copies = "1";
        object pages = "1";
        object pageType = Word.WdPrintOutPages.wdPrintAllPages;
        public string strUserName = "";
        object saveChange = Word.WdSaveOptions.wdDoNotSaveChanges;

        object missing = System.Reflection.Missing.Value;

        public WordHelper()
        {
            wordApp = new Word.Application();
        }

        #region 公开方法
        public void PrintAndSaveWord(object obj)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;

            object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\Test.docx";
            object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
            //File.Delete(pathNew.ToString());
            //File.Copy(fileName.ToString(), pathNew.ToString());

            copyFile(fileOld.ToString(), fileNew.ToString());

            Word.Document doc = wordApp.Documents.Open(
                ref fileNew, ref missingValue, ref readOnly, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue);
            wordApp.Visible = false;

            ReplaceAll(wordApp, "{0}", "Hello, Word!");

            BeginPrint(doc);
            //BeginSave(doc);
        }

        /// <summary>
        /// 开始打印派车单
        /// </summary>
        public bool ShortWithDriverPrint(Model.Company companInfo, Model.Customer customerInfo, ReserveSvc.Reserve reserveInfo, CarSvc.Car carInfo, out string msg)
        {
            Application.DoEvents();
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\ShortWithDriver.docx";
                //System.Windows.Forms.MessageBox.Show(fileOld.ToString());
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                //DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());

                //copyFile(fileOld.ToString(), fileNew.ToString());

                doc = wordApp.Documents.Open(
                    ref fileOld, ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{2}", companInfo.Tel);
                ReplaceAll(wordApp, "{3}", companInfo.Fax);
                ReplaceAll(wordApp, "{4}", companInfo.EMail);
                ReplaceAll(wordApp, "{5}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                ReplaceAll(wordApp, "{6}", reserveInfo.BookTime.ToString("yyyy-MM-dd"));
                ReplaceAll(wordApp, "{7}", reserveInfo.BookTime.ToString("HH:mm:ss"));
                ReplaceAll(wordApp, "{8}", reserveInfo.LivAddr);
                ReplaceAll(wordApp, "{9}", reserveInfo.AriAddr);
                ReplaceAll(wordApp, "{10}", reserveInfo.SenAddr);
                ReplaceAll(wordApp, "{11}", reserveInfo.FlayCode);
                ReplaceAll(wordApp, "{12}", customerInfo.ComName);
                ReplaceAll(wordApp, "{13}", customerInfo.Name);
                ReplaceAll(wordApp, "{14}", customerInfo.Tel);
                ReplaceAll(wordApp, "{15}", carInfo.Cartype);
                ReplaceAll(wordApp, "{16}", carInfo.Seat);
                ReplaceAll(wordApp, "{17}", reserveInfo.Rent.ToString());
                ReplaceAll(wordApp, "{18}", carInfo.DriverName);
                ReplaceAll(wordApp, "{19}", carInfo.Code);
                ReplaceAll(wordApp, "{20}", carInfo.DriverTel);
                ReplaceAll(wordApp, "{21}", reserveInfo.BookTime.ToString("yyyy-MM-dd HH:mm:ss"));
                ReplaceAll(wordApp, "{22}", reserveInfo.Marks);
                ReplaceAll(wordApp, "{23}", reserveInfo.Code);
                ReplaceAll(wordApp, "{24}", carInfo.DayLimitKM.ToString());
                ReplaceAll(wordApp, "{25}", carInfo.OvertimePrie.ToString());
                ReplaceAll(wordApp, "{26}", carInfo.TimeoutPrie.ToString());
                ReplaceAll(wordApp, "{27}", reserveInfo.CurrentMax.ToString());

                //BeginSave(doc);

                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
                msg = "";
            }
            catch (Exception ex)
            {
                msg = ex.Message + "---" + ex.ToString();
                result = false;
            }

            return result;
        }

        //打印合同--短租自驾
        public bool PrintContractShortWithoutDirver(Model.Company companInfo, Model.Customer customerInfo, ReserveSvc.Reserve reserveInfo, CarSvc.Car carInfo)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\ContractShortWithoutDirver.docx";
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                ////DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());

                doc = wordApp.Documents.Open(
                   ref fileOld, ref missingValue, ref readOnly, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", customerInfo.Name);
                ReplaceAll(wordApp, "{2}", customerInfo.Addr);
                ReplaceAll(wordApp, "{3}", customerInfo.CodeCar);
                ReplaceAll(wordApp, "{4}", customerInfo.Phone);
                ReplaceAll(wordApp, "{5}", customerInfo.DriCard);
                ReplaceAll(wordApp, "{6}", carInfo.Brand);
                ReplaceAll(wordApp, "{7}", carInfo.Cartype);
                ReplaceAll(wordApp, "{8}", carInfo.Code);
                //ReplaceAll(wordApp, "{29}", carInfo.FactoryOutDate.ToString("yyyy-MM-dd"));
                //string orderType = "";
                //if (reserveInfo.Type == 1 || reserveInfo.Type == 3)
                //    orderType = " 月";
                //else
                //    orderType = " 小时";
                ReplaceAll(wordApp, "{10}", reserveInfo.DeadLine.ToString() + reserveInfo.DeadLineType);
                ReplaceAll(wordApp, "{11}", reserveInfo.BookTime.Year.ToString());
                ReplaceAll(wordApp, "{12}", reserveInfo.BookTime.Month.ToString());
                ReplaceAll(wordApp, "{13}", reserveInfo.BookTime.Day.ToString());
                ReplaceAll(wordApp, "{9}", reserveInfo.BookTime.Hour.ToString());
                ReplaceAll(wordApp, "{32}", reserveInfo.BookTime.Minute.ToString());
                ReplaceAll(wordApp, "{14}", reserveInfo.EndBooktime.Year.ToString());
                ReplaceAll(wordApp, "{15}", reserveInfo.EndBooktime.Month.ToString());
                ReplaceAll(wordApp, "{16}", reserveInfo.EndBooktime.Day.ToString());
                ReplaceAll(wordApp, "{17}", reserveInfo.EndBooktime.Hour.ToString());
                ReplaceAll(wordApp, "{33}", reserveInfo.EndBooktime.Hour.ToString());
                ReplaceAll(wordApp, "{18}", reserveInfo.ZPrice.ToString() + "元/" + reserveInfo.DeadLineType);
                ReplaceAll(wordApp, "{19}", carInfo.OvertimePrie.ToString());
                ReplaceAll(wordApp, "{20}", reserveInfo.Deposit.ToString());
                ReplaceAll(wordApp, "{21}", reserveInfo.IllegalRetention.ToString());
                ReplaceAll(wordApp, "{22}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{23}", companInfo.Tel);
                ReplaceAll(wordApp, "{24}", carInfo.TimeoutPrie.ToString());
                ReplaceAll(wordApp, "{30}", reserveInfo.Code);
                ReplaceAll(wordApp, "{31}", reserveInfo.CurrentMax.ToString());
                ReplaceAll(wordApp, "{34}", carInfo.DayLimitKM.ToString());

                //BeginSave(doc);
                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        //打印合同--长租自驾
        public bool PrintContractLongWithoutDirver(Model.Company companInfo, Model.Customer customerInfo, ReserveSvc.Reserve reserveInfo, CarSvc.Car carInfo)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\ContractLongWithoutDirver.docx";
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                ////DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());

                doc = wordApp.Documents.Open(
                   ref fileOld, ref missingValue, ref readOnly, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", customerInfo.Name);
                ReplaceAll(wordApp, "{2}", customerInfo.Addr);
                ReplaceAll(wordApp, "{3}", customerInfo.CodeCar);
                ReplaceAll(wordApp, "{4}", customerInfo.Phone);
                ReplaceAll(wordApp, "{5}", customerInfo.DriCard);
                ReplaceAll(wordApp, "{6}", carInfo.Brand);
                ReplaceAll(wordApp, "{7}", carInfo.Cartype);
                ReplaceAll(wordApp, "{8}", carInfo.Code);
                //ReplaceAll(wordApp, "{29}", carInfo.FactoryOutDate.ToString("yyyy-MM-dd"));
                //string orderType = "";
                //if (reserveInfo.Type == 1 || reserveInfo.Type == 3)
                //    orderType = " 月";
                //else
                //    orderType = " 小时";
                ReplaceAll(wordApp, "{10}", reserveInfo.DeadLine.ToString() + reserveInfo.DeadLineType);
                ReplaceAll(wordApp, "{11}", reserveInfo.BookTime.Year.ToString());
                ReplaceAll(wordApp, "{12}", reserveInfo.BookTime.Month.ToString());
                ReplaceAll(wordApp, "{13}", reserveInfo.BookTime.Day.ToString());
                ReplaceAll(wordApp, "{9}", reserveInfo.BookTime.Hour.ToString());
                ReplaceAll(wordApp, "{32}", reserveInfo.BookTime.Minute.ToString());
                ReplaceAll(wordApp, "{14}", reserveInfo.EndBooktime.Year.ToString());
                ReplaceAll(wordApp, "{15}", reserveInfo.EndBooktime.Month.ToString());
                ReplaceAll(wordApp, "{16}", reserveInfo.EndBooktime.Day.ToString());
                ReplaceAll(wordApp, "{17}", reserveInfo.EndBooktime.Hour.ToString());
                ReplaceAll(wordApp, "{33}", reserveInfo.EndBooktime.Hour.ToString());
                ReplaceAll(wordApp, "{18}", reserveInfo.ZPrice.ToString() + "元/" + reserveInfo.DeadLineType);
                ReplaceAll(wordApp, "{19}", carInfo.OvertimePrie.ToString());
                ReplaceAll(wordApp, "{20}", reserveInfo.Deposit.ToString());
                ReplaceAll(wordApp, "{21}", reserveInfo.IllegalRetention.ToString());
                ReplaceAll(wordApp, "{22}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{23}", companInfo.Tel);
                ReplaceAll(wordApp, "{24}", carInfo.TimeoutPrie.ToString());
                ReplaceAll(wordApp, "{30}", reserveInfo.Code);
                ReplaceAll(wordApp, "{31}", reserveInfo.CurrentMax.ToString());
                ReplaceAll(wordApp, "{34}", carInfo.MonthLimitKM.ToString());

                //BeginSave(doc);
                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        //打印合同--长租带驾
        public bool PrintContractLongWithDirver(Model.Company companInfo, Model.Customer customerInfo, ReserveSvc.Reserve reserveInfo, CarSvc.Car carInfo)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\ContractLongWithDirver.docx";
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                ////DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());

                doc = wordApp.Documents.Open(
                   ref fileOld, ref missingValue, ref readOnly, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", customerInfo.Name);
                ReplaceAll(wordApp, "{2}", customerInfo.Addr);
                ReplaceAll(wordApp, "{3}", customerInfo.CodeCar);
                ReplaceAll(wordApp, "{4}", customerInfo.Phone);
                ReplaceAll(wordApp, "{5}", customerInfo.DriCard);
                ReplaceAll(wordApp, "{6}", carInfo.Brand);
                ReplaceAll(wordApp, "{7}", carInfo.Cartype);
                ReplaceAll(wordApp, "{8}", carInfo.Code);
                //ReplaceAll(wordApp, "{29}", carInfo.FactoryOutDate.ToString("yyyy-MM-dd"));
                //string orderType = "";
                //if (reserveInfo.Type == 1 || reserveInfo.Type == 3)
                //    orderType = " 月";
                //else
                //    orderType = " 小时";
                ReplaceAll(wordApp, "{10}", reserveInfo.DeadLine.ToString() + reserveInfo.DeadLineType);
                ReplaceAll(wordApp, "{11}", reserveInfo.BookTime.Year.ToString());
                ReplaceAll(wordApp, "{12}", reserveInfo.BookTime.Month.ToString());
                ReplaceAll(wordApp, "{13}", reserveInfo.BookTime.Day.ToString());
                ReplaceAll(wordApp, "{9}", reserveInfo.BookTime.Hour.ToString());
                ReplaceAll(wordApp, "{32}", reserveInfo.BookTime.Minute.ToString());
                ReplaceAll(wordApp, "{14}", reserveInfo.EndBooktime.Year.ToString());
                ReplaceAll(wordApp, "{15}", reserveInfo.EndBooktime.Month.ToString());
                ReplaceAll(wordApp, "{16}", reserveInfo.EndBooktime.Day.ToString());
                ReplaceAll(wordApp, "{17}", reserveInfo.EndBooktime.Hour.ToString());
                ReplaceAll(wordApp, "{33}", reserveInfo.EndBooktime.Hour.ToString());
                ReplaceAll(wordApp, "{18}", reserveInfo.ZPrice.ToString() + "元/" + reserveInfo.DeadLineType);
                ReplaceAll(wordApp, "{19}", carInfo.OvertimePrie.ToString());
                ReplaceAll(wordApp, "{20}", reserveInfo.Deposit.ToString());
                ReplaceAll(wordApp, "{21}", reserveInfo.IllegalRetention.ToString());
                ReplaceAll(wordApp, "{22}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{23}", companInfo.Tel);
                ReplaceAll(wordApp, "{24}", carInfo.TimeoutPrie.ToString());
                ReplaceAll(wordApp, "{30}", reserveInfo.Code);
                ReplaceAll(wordApp, "{31}", reserveInfo.CurrentMax.ToString());
                ReplaceAll(wordApp, "{34}", carInfo.MonthLimitKM.ToString());

                //BeginSave(doc);
                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        

        //打印订单信息
        public bool PrintOrderInfoView(Model.Company companInfo, DataSet dsCars)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\AllOrdersList.docx";
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                ////DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());


                DataRow dr = dsCars.Tables[0].Rows[0];

                string type = dr["Type"].ToString();
                string drivetype = string.Empty;
                switch (type)
                {
                    case "1":
                        drivetype = "长租自驾";
                        break;
                    case "2":
                        drivetype = "短租自驾";
                        break;
                    case "3":
                        drivetype = "长租带驾";
                        break;
                    case "4":
                        drivetype = "短租带驾";
                        break;
                }

                doc = wordApp.Documents.Open(
                   ref fileOld, ref missingValue, ref readOnly, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", dr["OrderCode"].ToString());
                ReplaceAll(wordApp, "{2}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{3}", companInfo.Tel);
                ReplaceAll(wordApp, "{26}", Convert.ToDateTime(dr["InpuTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));

                ReplaceAll(wordApp, "{4}", dr["CustomerCode"].ToString());
                ReplaceAll(wordApp, "{5}", dr["Custype"].ToString().Trim() == "1" ? "个人" : "企业");
                ReplaceAll(wordApp, "{6}", dr["ComName"].ToString());
                ReplaceAll(wordApp, "{7}", dr["Name"].ToString());
                ReplaceAll(wordApp, "{8}", dr["Tel"].ToString());
                ReplaceAll(wordApp, "{9}", dr["Phone"].ToString());
                ReplaceAll(wordApp, "{10}", dr["Brand"].ToString());
                ReplaceAll(wordApp, "{11}", dr["Cartype"].ToString());
                ReplaceAll(wordApp, "{12}", dr["Code"].ToString());
                ReplaceAll(wordApp, "{13}", dr["Color"].ToString());
                ReplaceAll(wordApp, "{14}", drivetype);
                ReplaceAll(wordApp, "{15}", dr["DayRent"].ToString());
                ReplaceAll(wordApp, "{16}", dr["LivAddr"].ToString());
                ReplaceAll(wordApp, "{17}", dr["AriAddr"].ToString());
                ReplaceAll(wordApp, "{18}", Convert.ToDateTime(dr["BookTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                ReplaceAll(wordApp, "{19}", Convert.ToDateTime(dr["EndBooktime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                ReplaceAll(wordApp, "{20}", dr["TimeoutPrie"].ToString());
                ReplaceAll(wordApp, "{21}", dr["OvertimePrie"].ToString());
                ReplaceAll(wordApp, "{22}", dr["DayLimitKM"].ToString());
                ReplaceAll(wordApp, "{23}", dr["DriverName"].ToString());
                ReplaceAll(wordApp, "{24}", dr["OrderInputName"].ToString());

                //BeginSave(doc);
                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
            }
            catch (Exception ex)
            {
                result = false;
                System.Windows.Forms.MessageBox.Show(ex.Message + "<---------->" + ex.ToString());
            }
            return result;
        }

        //打印结算单
        public bool PrintSettlement(Model.Company companInfo, Model.Customer customerInfo, ReserveSvc.Reserve reserveInfo, CarSvc.Car carInfo, bool isFinish, decimal totalAmount, string userName, decimal earlyRefund)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\还车结算单.docx";
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                ////DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());

                doc = wordApp.Documents.Open(
                   ref fileOld, ref missingValue, ref readOnly, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", reserveInfo.Code);
                ReplaceAll(wordApp, "{2}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{3}", companInfo.Tel);
                ReplaceAll(wordApp, "{4}", reserveInfo.SettlementDate.Year.ToString());
                ReplaceAll(wordApp, "{5}", reserveInfo.SettlementDate.Month.ToString());
                ReplaceAll(wordApp, "{6}", reserveInfo.SettlementDate.Day.ToString());
                ReplaceAll(wordApp, "{7}", customerInfo.Code);
                ReplaceAll(wordApp, "{8}", customerInfo.ComName);
                ReplaceAll(wordApp, "{9}", customerInfo.Name);
                ReplaceAll(wordApp, "{10}", carInfo.Code);
                ReplaceAll(wordApp, "{11}", reserveInfo.RentReceivable.ToString());
                ReplaceAll(wordApp, "{12}", reserveInfo.ReceivedAmount.ToString());
                ReplaceAll(wordApp, "{13}", reserveInfo.OvertimeAmount.ToString());
                ReplaceAll(wordApp, "{14}", reserveInfo.SuperDriverAmount.ToString());
                ReplaceAll(wordApp, "{15}", reserveInfo.TotalFines.ToString());
                ReplaceAll(wordApp, "{16}", reserveInfo.DepositReturn.ToString());
                ReplaceAll(wordApp, "{17}", reserveInfo.IllegalRetention.ToString());
                ReplaceAll(wordApp, "{18}", reserveInfo.RepairDamage.ToString());
                ReplaceAll(wordApp, "{19}", reserveInfo.MealsAccommodation.ToString());

                ReplaceAll(wordApp, "{21}", userName);
                //ReplaceAll(wordApp, "{22}", "");
                ReplaceAll(wordApp, "{23}", reserveInfo.IllegalRetention.ToString());
                if (isFinish)
                {
                    ReplaceAll(wordApp, "{20}", totalAmount.ToString());
                    ReplaceAll(wordApp, "{25}", "0");
                }
                else
                {
                    ReplaceAll(wordApp, "{20}", "0");
                    ReplaceAll(wordApp, "{25}", totalAmount.ToString());
                }
                ReplaceAll(wordApp, "{26}", earlyRefund.ToString());
                //BeginSave(doc);
                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        //打印位置保留退款单
        public bool PrintReturnIllegalInfo(DataSet ds, decimal returnAmount, Model.Company companInfo)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\Temp001.docx";
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                ////DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());

                doc = wordApp.Documents.Open(
                   ref fileOld, ref missingValue, ref readOnly, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", ds.Tables[0].Rows[0]["ReserveCode"].ToString());
                ReplaceAll(wordApp, "{2}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{3}", companInfo.Tel);
                ReplaceAll(wordApp, "{4}", DateTime.Now.Year.ToString());
                ReplaceAll(wordApp, "{5}", DateTime.Now.Month.ToString());
                ReplaceAll(wordApp, "{6}", DateTime.Now.Day.ToString());
                ReplaceAll(wordApp, "{7}", ds.Tables[0].Rows[0]["Code"].ToString());
                ReplaceAll(wordApp, "{8}", ds.Tables[0].Rows[0]["ComName"].ToString());
                ReplaceAll(wordApp, "{9}", ds.Tables[0].Rows[0]["Name"].ToString());
                ReplaceAll(wordApp, "{10}", ds.Tables[0].Rows[0]["CarCode"].ToString());
                ReplaceAll(wordApp, "{11}", ds.Tables[0].Rows[0]["IllegalRetention"].ToString());
                string IllSumAmount = ds.Tables[0].Rows[0]["IllegalSumAmount"].ToString();
                if (string.IsNullOrEmpty(IllSumAmount))
                    IllSumAmount = "0";
                IllSumAmount = "-" + IllSumAmount;
                ReplaceAll(wordApp, "{12}", IllSumAmount);
                ReplaceAll(wordApp, "{13}", returnAmount.ToString());
                ReplaceAll(wordApp, "{14}", Login.LogTrueName);

                //BeginSave(doc);
                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        //打印补款凭单
        public bool PrintMakeMoneyRenewal(Model.Company companInfo, Model.Customer customerInfo, ReserveSvc.Reserve reserveInfo, BillingDetailsSvc.BillingDetails BillInfo, string userName, string oldDate)
        {
            //DataCenterInspection info = (DataCenterInspection)obj;
            Word.Document doc = null;
            bool result = true;
            try
            {
                object fileOld = Environment.CurrentDirectory + @"\PrintExport\WordTemplates\Temp002.docx";
                //object fileNew = Environment.CurrentDirectory + @"\DocumentsTemp\" + getNewFileName() + ".docx";
                //File.Delete(fileNew.ToString());
                ////DeleteAllTempFiles();
                //File.Copy(fileOld.ToString(), fileNew.ToString());

                doc = wordApp.Documents.Open(
                   ref fileOld, ref missingValue, ref readOnly, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue, ref missingValue, ref missingValue,
                   ref missingValue, ref missingValue, ref missingValue);
                wordApp.Visible = false;

                ReplaceAll(wordApp, "{0}", companInfo.ComName);
                ReplaceAll(wordApp, "{1}", reserveInfo.Code);
                ReplaceAll(wordApp, "{2}", companInfo.ComAddr);
                ReplaceAll(wordApp, "{3}", companInfo.Tel);
                ReplaceAll(wordApp, "{4}", BillInfo.InpuTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                ReplaceAll(wordApp, "{5}", customerInfo.Code);
                ReplaceAll(wordApp, "{6}", customerInfo.ComName);
                ReplaceAll(wordApp, "{7}", customerInfo.Name);
                ReplaceAll(wordApp, "{8}", BillInfo.IntoSectionType);
                ReplaceAll(wordApp, "{9}", BillInfo.CollectionType);
                ReplaceAll(wordApp, "{10}", BillInfo.ImportBank);
                ReplaceAll(wordApp, "{11}", BillInfo.ImportBankAccount);
                ReplaceAll(wordApp, "{12}", BillInfo.ImportAmount.ToString());
                ReplaceAll(wordApp, "{13}", oldDate);
                ReplaceAll(wordApp, "{14}", BillInfo.EndBookDate.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                ReplaceAll(wordApp, "{15}", userName);
                //BeginSave(doc);
                BeginPrint(doc);
                //File.Delete(fileNew.ToString());
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        #endregion

        #region 私有方法

        private void DeleteAllTempFiles()
        {
            string fileOld = Environment.CurrentDirectory + @"\DocumentsTemp";
            DirectoryInfo dirs = new DirectoryInfo(fileOld);
            FileInfo[] files = dirs.GetFiles();

            foreach (FileInfo info in files)
                info.Delete();

        }

        private void copyFile(string fileOld, string fileNew)
        {
            //定义一个文件流,用于读取原文件
            FileStream fs = new FileStream(fileOld.ToString(), FileMode.Open);
            //定义内存缓冲区
            byte[] buffer = new byte[(int)fs.Length];
            //将文件读入到内存缓冲区中
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();

            //定义一个文件流,用于将缓冲区中的文件流写入文件
            FileStream fs1 = new FileStream(fileNew.ToString(), FileMode.Create);
            //写入文件
            fs1.Write(buffer, 0, buffer.Length);
            fs1.Close();
        }

        private void ReplaceAll(Word.Application wordApp, string strOldText, string strNewText)
        {
            object replaceAll = Word.WdReplace.wdReplaceAll;
            wordApp.Selection.Find.ClearFormatting();
            wordApp.Selection.Find.Text = strOldText;
            wordApp.Selection.Find.Replacement.ClearFormatting();
            wordApp.Selection.Find.Replacement.Text = strNewText;
            wordApp.Selection.Find.Execute(ref missingValue,
                ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue, ref missingValue,
                ref replaceAll, ref missingValue, ref missingValue,
                ref missingValue, ref missingValue);
        }

        private void BeginPrint(Word.Document doc)
        {
            try
            {

                doc.PrintOut(ref myTrue, ref myFalse, ref range,
                               ref missingValue, ref missingValue, ref missingValue,
                               ref items, ref copies, ref pages, ref pageType, ref myFalse,
                               ref myTrue, ref missingValue, ref myFalse, ref missingValue,
                           ref missingValue, ref missingValue, ref missingValue);
            }
            catch (Exception ex)
            {
                Common.PageCommon.Alert(ex.Message);
            }
            finally
            {
                if (doc != null)
                    doc.Close(ref saveChange, ref missing, ref missing);
                if (wordApp != null)
                    wordApp.Quit(ref saveChange, ref missing, ref missing);
            }

        }

        private void BeginSave(Word.Document doc)
        {
            try
            {
                doc.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "------" + ex.StackTrace);
            }
        }

        private string getNewFileName()
        {
            return Login.LogTrueName + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
        }

        public decimal ComputeToF3(decimal originalValue)
        {
            string valueStr = originalValue.ToString("F8");
            //看第5位, 如果是奇数,不进位，否则第四位减1
            int charValue = int.Parse(valueStr.Substring(valueStr.Length - 4, 1));
            decimal result;
            if (charValue % 2 > 0)
                result = decimal.Parse(valueStr.Substring(0, valueStr.Length - 4));
            else
                result = decimal.Parse(valueStr.Substring(0, valueStr.Length - 4)) - 0.0001M;

            //四舍五入
            return decimal.Round(result, 3, MidpointRounding.AwayFromZero);
        }

        #endregion
    }
}
